// =====================================================================
//
//  This file is part of the Microsoft Dynamics CRM SDK code samples.
//
//  Copyright (C) Microsoft Corporation.  All rights reserved.
//
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
// =====================================================================
using System;
using System.Runtime.Serialization;
using Microsoft.Xrm.Sdk.Temporary;

namespace Microsoft.Xrm.Sdk
{
	public static class FieldPermissionType
	{
		// the values here are 0 and 4 and these need to be in sync with the option set field_security_permission_type
		// we intentionally left out 1, 2 and 3 so that we can add support for them in upcoming releases
		// if these were to mirror privileges in a role, then the values would mean:
		// 0 - No Access
		// 1 - Owner
		// 2 - Local
		// 3 - Deep
		// 4 - Global
		public const int NotAllowed = 0;
		public const int Allowed = 4;

		/// <summary>
		/// Validate whether the provided value is one of the constants in the class
		/// </summary>
		/// <exception cref="ArgumentOutOfRangeException">Throws ArgumentOutOfRangeException if the provided value is not 
		/// one of the constants specified in the class</exception>
		/// <param name="value">an integer to be validated as a FieldPermissionType</param>
		public static void Validate(int value)
		{
			if (value != Allowed && value != NotAllowed)
			{
				throw new ArgumentOutOfRangeException("value",
					String.Format(System.Globalization.CultureInfo.InvariantCulture, "Value {0} is not a valid FieldPermissionType", value));
			}
		}
	}

	[DataContract(Name = "AttributePrivilege", Namespace = XmlNamespaces.V5.Contracts)]
	[Serializable]
	public sealed class AttributePrivilege : IExtensibleDataObject
	{
		#region Fields
		private Guid _attributeId;
		private int _canCreate;
		private int _canRead;
		private int _canUpdate;
		#endregion Fields
		public AttributePrivilege()
		{
		}

		public AttributePrivilege(Guid attributeId, int canCreate, int canRead, int canUpdate)
		{
			_attributeId = attributeId;
			_canCreate = canCreate;
			_canRead = canRead;
			_canUpdate = canUpdate;
		}

		[DataMember]
		public Guid AttributeId
		{
			get { return this._attributeId; }
			internal set { this._attributeId = value; }
		}

		[DataMember]
		public int CanCreate
		{
			get { return _canCreate; }
			internal set { this._canCreate = value; }
		}

		[DataMember]
		public int CanRead
		{
			get { return _canRead; }
			internal set { this._canRead = value; }
		}

		[DataMember]
		public int CanUpdate
		{
			get { return _canUpdate; }
			internal set { this._canUpdate = value; }
		}

		#region IExtensibleDataObject Implementation
		[NonSerialized]
		private ExtensionDataObject ExtensionDataObject;
		public ExtensionDataObject ExtensionData
		{
			get
			{
				return ExtensionDataObject;
			}
			set
			{
				ExtensionDataObject = value;
			}
		}
		#endregion IExtensibleDataObject Implementation
	}
}
